package com.ning.weixin.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.ning.weixin.entity.AcShareLog;

/**
 * @author MENG_LI_DONG
 * @version 2018年12月18日 下午10:29:44
 */
@Component
public class AcShareLogDao {
	private Logger log = LoggerFactory.getLogger(AcShareLogDao.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public void insert(AcShareLog t){
		final StringBuffer sql = new StringBuffer();
		StringBuffer sqlParams = new StringBuffer();
		final List<Object> params = new ArrayList<>();
		sql.append("insert into ac_share_log(create_time,update_time,");
		sqlParams.append("?,?,");
		params.add(new Date());
		params.add(params.get(0));
		if(t.getUserId()!=null){
			sql.append("user_id,");
			sqlParams.append("?,");
			params.add(t.getUserId());
		}
		if(t.getOpenid()!=null){
			sql.append("openid,");
			sqlParams.append("?,");
			params.add(t.getOpenid());
		}
		
		if(t.getLocationId()!=null){
			sql.append("location_id,");
			sqlParams.append("?,");
			params.add(t.getLocationId());
		}
		sql.deleteCharAt(sql.length() - 1);
		sqlParams.deleteCharAt(sqlParams.length() - 1);
		sql.append(") values(").append(sqlParams).append(")");
		log.debug("sql:" + sql);
		try {
			jdbcTemplate.update(sql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
}
